<?php
defined('_JEXEC') or die('Restricted access');

class igUploadPlupload
{
	function pluploadHeadJs()
	{
		$catid = JRequest::getInt('catid',0);
		$Itemid = JRequest::getInt('Itemid', null);
		$ItemIdString = empty($Itemid) ? '' : '&Itemid='.$Itemid;
		
		$configArray =& JComponentHelper::getParams('com_igallery');
		$resize = $configArray->get('client_resize', 0);
		$resizeWidth = $configArray->get('client_resize_width', 2000);
		$resizeHeight = $configArray->get('client_resize_height', 1500);
		$resizeQuality = $configArray->get('client_resize_quality', 100);
		$moderateString = $configArray->get('moderate_img', 0) == 0 || IG_BACKEND == true ? '' : '&moderateMsg=1';
		
		$document =& JFactory::getDocument();
		$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/plupload/js/jquery-1.3.2.js');
		$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/plupload/js/plupload.min.js');
		$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/plupload/js/jquery.plupload.queue.min.js');
		$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/plupload/js/plupload.html5.min.js');
		$document->addScript(IG_HOST.'administrator/components/com_igallery/lib/uploaders/plupload/js/plupload.flash.min.js');
		$document->addStyleSheet(IG_HOST.'administrator/components/com_igallery/lib/uploaders/plupload/css/plupload.queue.css');
		
		$headJs = '
		var $j = jQuery.noConflict();
		
		$j(function()
		{
			jQuery("#plupload_div").pluploadQueue(
			{
				runtimes : \'html5,flash\',
				url : \'index.php?option=com_igallery&controller=igphoto&task=plUpload&catid='.$catid.'&format=raw\',
				max_file_size : \'10mb\',
				unique_names : false,
				preinit: attachCallbacks,
				multipart: true,
				filters : [{title : "Image files", extensions : "jpg,jpeg,gif,png"}],
				flash_swf_url : \''.IG_HOST.'administrator/components/com_igallery/lib/uploaders/plupload/js/plupload.flash.swf\',
				';
				if($resize == 1)
				{
				$headJs .= '
		 		resize : {width : '.$resizeWidth.', height : '.$resizeHeight.', quality : '.$resizeQuality.'}';
				}
				$headJs .= '
			});
		});
		
		function attachCallbacks(Uploader)
		{
			Uploader.bind(\'FileUploaded\', function(Up, File, Response)
			{
				if(Response.response.length > 2)
				{
		        	alert(\'Error, response from server: \' + Response.response);
		        	Up.trigger("Error", {message : Response.response, code : 9999, details : Response.response, file: File});
		        	Uploader.stop();
		        	return false;
				}
					
				if( (Uploader.total.uploaded + 1) >= Uploader.files.length)
				{
					window.location = \'index.php?option=com_igallery&controller=igphoto&view=igphoto&catid='.$catid.$ItemIdString.$moderateString.'\';
				}
		
	    	});
		};
		';
		$document->addScriptDeclaration($headJs);		
	}
	
	function pluploadHTML()
	{
		?>
		<div id="plupload_div" style="width: 450px; height: 330px;">Plupload Initialise Error.</div>
		<?php
	}
}